package com.cmcc.passenger.ui.fragment

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.cmcc.passenger.R
import com.cmcc.passenger.databinding.DialogTaxiCancelBinding
import com.cmcc.shared.utils.clickWithTrigger

class AppAlertDialog(context: Context) {

    private val binding: DialogTaxiCancelBinding =
        DialogTaxiCancelBinding.inflate(LayoutInflater.from(context), null, false)

    private val dialog = Dialog(context, R.style.TransparentStyleBottomDialog)

    init {
        //        val inflater = LayoutInflater.from(context)
        dialog.setContentView(binding.root)
        if (context is Activity) {
            val m = context.windowManager
            val d = m.defaultDisplay
            val p = dialog.window!!.attributes
            p.width = (d.width * 0.9).toInt()
            dialog.window!!.attributes = p
        }

    }

    fun setContent(content: String): AppAlertDialog {
        binding.content.text = content
        return this
    }

    fun setNavigation(
        text: String,
        listener: View.OnClickListener?,
        visible: Boolean = true
    ): AppAlertDialog {
        binding.navigationButton.text = text
        binding.navigationButton.visibility = if (visible) View.VISIBLE else View.GONE
        binding.navigationButton.clickWithTrigger { view ->
            run {
                listener?.onClick(view)
                dialog.dismiss()
            }
        }
        return this
    }

    fun setPositive(text: String, listener: View.OnClickListener?): AppAlertDialog {
        binding.positiveButton.text = text
        binding.positiveButton.clickWithTrigger { view ->
            run {
                listener?.onClick(view)
                dialog.dismiss()
            }
        }
        return this
    }

    fun show() {
        if (!dialog.isShowing)
            dialog.show()
    }

    fun dismiss() {
        dialog.dismiss()
    }


}